package dk.au.cs.peterbn.patterns.impl;

import dk.au.cs.peterbn.patterns.CallSite;
import dk.au.cs.peterbn.patterns.DesignPattern;
import dk.au.cs.peterbn.patterns.MemberRole;

import java.util.HashSet;
import java.util.Set;

/**
 * Default implementation of MemberRole
 */
public class MemberRoleImpl implements MemberRole, HasModifiableDescription{
    private String description;
    private final String name;
    private final DesignPattern pattern;
    private Set<CallSite> callSites;

    public MemberRoleImpl(DesignPattern pattern, String name) {
        this.pattern = pattern;
        this.name = name;
        this.description = "";
        this.callSites = new HashSet<CallSite>();
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    @Override
    public Set<CallSite> getCallSites() {
        return callSites;
    }

    @Override
    public boolean isRequired() {
        return true;
    }

    @SuppressWarnings({"RedundantIfStatement"})
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MemberRoleImpl that = (MemberRoleImpl) o;

        if (!name.equals(that.name)) return false;
        if (!pattern.equals(that.pattern)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + pattern.hashCode();
        return result;
    }

    public String getDescription() {
        return description;
    }

    public void clearCallSites() {
        callSites = new HashSet<CallSite>();
    }

    public void addCallSite(CallSiteImpl site) {
        callSites.add(site);
    }
}
